package web.controller.address_telefon;

import hibernate.pojo.User;
import hibernate.util.HibernateUtil;

import java.io.Serializable;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

public class EditUserAccountController{

	private static final Log logger = LogFactory.getLog(EditUserAccountController.class);
	
	public EditUserAccountController() {
		// TODO Auto-generated constructor stub
	}
	
	private User user;
	private String userFirstName;
	private String userLastName;

	public String updateUserInformation(){
		String pageToRedirect = "";
		
		logger.info(userFirstName);
		logger.info(userLastName);
		
		try {
			
			FacesContext context = FacesContext.getCurrentInstance();
			user = (User)context.getExternalContext().getSessionMap().get("user");
			
			user.setFirstName(userFirstName);
			user.setLastName(userLastName);
			
			Session session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();
			session.update(user);
			transaction.commit();
		
			pageToRedirect = "editUserInfoSuccess";
		} catch (Exception exception) {
			logger.error("Error Occured During User Info Update :", exception);
		}		
		
		return pageToRedirect;
	}
	
	public String backToMain(){
		
		return "backToMain";
		
	}	
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String getUserFirstName() {
		return userFirstName;
	}
	public void setUserFirstName(String userFirstName) {
		this.userFirstName = userFirstName;
	}
	public String getUserLastName() {
		return userLastName;
	}
	public void setUserLastName(String userLastName) {
		this.userLastName = userLastName;
	}
}
